<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="广告账号id" prop="advertiserId">
        <el-input v-model="formData.advertiserId" placeholder="请输入广告账号id" />
      </el-form-item>
      <el-form-item label="广告账号名称" prop="advertiserName">
        <el-input v-model="formData.advertiserName" placeholder="请输入广告账号名称" />
      </el-form-item>
      <el-form-item label="商品id" prop="productId">
        <el-input v-model="formData.productId" placeholder="请输入商品id" />
      </el-form-item>
      <el-form-item label="当日时间戳" prop="thatdayTimestamp">
        <el-input v-model="formData.thatdayTimestamp" placeholder="请输入当日时间戳" />
      </el-form-item>
      <el-form-item label="当日日期" prop="thatdayDate">
        <el-date-picker
          v-model="formData.thatdayDate"
          type="date"
          value-format="x"
          placeholder="选择当日日期"
        />
      </el-form-item>
      <el-form-item label="当日总收入" prop="thatdayTotalAmount">
        <el-input v-model="formData.thatdayTotalAmount" placeholder="请输入当日总收入" />
      </el-form-item>
      <el-form-item label="当日总支付笔数" prop="thatdayPaidTotalNum">
        <el-input v-model="formData.thatdayPaidTotalNum" placeholder="请输入当日总支付笔数" />
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <el-input v-model="formData.area" placeholder="请输入地区" />
      </el-form-item>
      <el-form-item label="广告花费金额（美元）" prop="spend">
        <el-input v-model="formData.spend" placeholder="请输入广告花费金额（美元）" />
      </el-form-item>
      <el-form-item label="广告花费金额（外币）" prop="spendForeign">
        <el-input v-model="formData.spendForeign" placeholder="请输入广告花费金额（外币）" />
      </el-form-item>
      <el-form-item label="展示数" prop="impressions">
        <el-input v-model="formData.impressions" placeholder="请输入展示数" />
      </el-form-item>
      <el-form-item label="千次展示成本=总消耗/展示数*1000" prop="cpm">
        <el-input v-model="formData.cpm" placeholder="请输入千次展示成本=总消耗/展示数*1000" />
      </el-form-item>
      <el-form-item label="点击数" prop="clicks">
        <el-input v-model="formData.clicks" placeholder="请输入点击数" />
      </el-form-item>
      <el-form-item label="点击率" prop="ctr">
        <el-input v-model="formData.ctr" placeholder="请输入点击率" />
      </el-form-item>
      <el-form-item label="单次点击成本=总花费/点击数" prop="cpc">
        <el-input v-model="formData.cpc" placeholder="请输入单次点击成本=总花费/点击数" />
      </el-form-item>
      <el-form-item label="链接点击数" prop="inlineLinkClicks">
        <el-input v-model="formData.inlineLinkClicks" placeholder="请输入链接点击数" />
      </el-form-item>
      <el-form-item label="链接点击的平均成本=花费/链接点击次数" prop="costPerInlineLinkClick">
        <el-input v-model="formData.costPerInlineLinkClick" placeholder="请输入链接点击的平均成本=花费/链接点击次数" />
      </el-form-item>
      <el-form-item label="预估1000人平均成本" prop="cpp">
        <el-input v-model="formData.cpp" placeholder="请输入预估1000人平均成本" />
      </el-form-item>
      <el-form-item label="覆盖人数" prop="reach">
        <el-input v-model="formData.reach" placeholder="请输入覆盖人数" />
      </el-form-item>
      <el-form-item label="频次" prop="frequency">
        <el-input v-model="formData.frequency" placeholder="请输入频次" />
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入部门id" />
      </el-form-item>
      <el-form-item label="创建者名称" prop="createName">
        <el-input v-model="formData.createName" placeholder="请输入创建者名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EverdayCostApi, EverdayCostVO } from '@/api/ad/everdaycost'

/** facebook广告账户每日消耗 表单 */
defineOptions({ name: 'EverdayCostForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  costId: undefined,
  advertiserId: undefined,
  advertiserName: undefined,
  productId: undefined,
  thatdayTimestamp: undefined,
  thatdayDate: undefined,
  thatdayTotalAmount: undefined,
  thatdayPaidTotalNum: undefined,
  area: undefined,
  spend: undefined,
  spendForeign: undefined,
  impressions: undefined,
  cpm: undefined,
  clicks: undefined,
  ctr: undefined,
  cpc: undefined,
  inlineLinkClicks: undefined,
  costPerInlineLinkClick: undefined,
  cpp: undefined,
  reach: undefined,
  frequency: undefined,
  deptId: undefined,
  createName: undefined,
})
const formRules = reactive({
  advertiserId: [{ required: true, message: '广告账号id不能为空', trigger: 'blur' }],
  advertiserName: [{ required: true, message: '广告账号名称不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '商品id不能为空', trigger: 'blur' }],
  thatdayTimestamp: [{ required: true, message: '当日时间戳不能为空', trigger: 'blur' }],
  thatdayDate: [{ required: true, message: '当日日期不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await EverdayCostApi.getEverdayCost(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as EverdayCostVO
    if (formType.value === 'create') {
      await EverdayCostApi.createEverdayCost(data)
      message.success(t('common.createSuccess'))
    } else {
      await EverdayCostApi.updateEverdayCost(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    costId: undefined,
    advertiserId: undefined,
    advertiserName: undefined,
    productId: undefined,
    thatdayTimestamp: undefined,
    thatdayDate: undefined,
    thatdayTotalAmount: undefined,
    thatdayPaidTotalNum: undefined,
    area: undefined,
    spend: undefined,
    spendForeign: undefined,
    impressions: undefined,
    cpm: undefined,
    clicks: undefined,
    ctr: undefined,
    cpc: undefined,
    inlineLinkClicks: undefined,
    costPerInlineLinkClick: undefined,
    cpp: undefined,
    reach: undefined,
    frequency: undefined,
    deptId: undefined,
    createName: undefined,
  }
  formRef.value?.resetFields()
}
</script>
